﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
namespace Data
{
    public class BaoCaoTonRepository
    {
        public static bool Exists(DateTime thang)
        {
            NhaSachEntities context = new NhaSachEntities();

            var q = (from bc in context.BaoCaoTonList
                     where ((DateTime)bc.Thang).Month == thang.Month && ((DateTime)bc.Thang).Year == thang.Year
                     select bc).Count();
            if (q > 0)
                return true;
            return false;

        }
        public static BaoCaoTonDTO GetBaoCaoTonByThang(DateTime thang)
        {
            NhaSachEntities context = new NhaSachEntities();

            var q = (from bc in context.BaoCaoTonList
                     where ((DateTime)bc.Thang).Month == thang.Month && ((DateTime)bc.Thang).Year == thang.Year
                     select new BaoCaoTonDTO(){
                         MaBaoCaoTon=bc.MaBaoCaoTon,
                         Thang=(DateTime)bc.Thang,
                         TinhTrang=(int)bc.TinhTrang
                     }).FirstOrDefault();
            return q;
        }
        public static int GetCurrentID()
        {
            NhaSachEntities context=new NhaSachEntities();
            Nullable<int> id = context.GetCurrentID("BaoCaoTon").FirstOrDefault();
            return (int)id;
        }

        public static void Insert(BaoCaoTonDTO baoCaoTon)
        {
            NhaSachEntities context = new NhaSachEntities();
            BaoCaoTon bc = new BaoCaoTon()
            {
                Thang=baoCaoTon.Thang,
                TinhTrang=baoCaoTon.TinhTrang
            };
            context.AddToBaoCaoTonList(bc);
            context.SaveChanges();
        }
            
    }
}
